package com.hare.common.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapUtils {
    /**
     * 转换request 请求参数
     *
     * @param paramMap request获取的参数数组
     */
    public static Map<String, String> convertMap(Map<String, String[]> paramMap) {
        Map<String, String> rtnMap = new HashMap<String, String>();
        for (String key : paramMap.keySet()) {
            rtnMap.put(key, paramMap.get(key)[0]);
        }
        return rtnMap;
    }

    /**
     * 转换 querystring
     *
     * @param map
     * @return a=b&b=c&c=d
     */
    public static String toQuerystring(Map<String, String> map) {
        if (CollUtil.isEmpty(map)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry) iterator.next();
            if (StrUtil.isNotBlank(entry.getValue())) {
                sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
            }
        }
        return sb.substring(1);
    }
}
